import pygame
import sys #退出程序时用到
from pygame.locals import *

pygame.init()#初始化pygame
print(pygame.display.list_modes())

size = width,height = 600,400
speed = [2,1]
bg = (255,255,255)
fullscreen = False

clock = pygame.time.Clock()

screen = pygame.display.set_mode(size,RESIZABLE)#窗口大小，可变
pygame.display.set_caption('Hello!')#窗口标题
thing = pygame.image.load('yd.png')#加载图片
position = thing.get_rect()#获得图像位置矩形

r_head = thing
l_head = pygame.transform.flip(thing,True,False)#翻转图像

while True:
    for event in pygame.event.get():#事件队列循环
        if event.type == pygame.QUIT:
             sys.exit()#系统退出

        if event.type == KEYDOWN:
            if event.key == K_LEFT:#是key不是type!!!
                thing = l_head
                speed = [-1,0]
            if event.key == K_RIGHT:
                thing = r_head
                speed = [1,0]
            if event.key == K_UP:
                speed = [0,-1]
            if event.key == K_DOWN:
                speed = [0,1]

            if event.key == K_F11:#全屏
                fullscreen = not fullscreen
                if fullscreen:
                    screen = pygame.display.set_mode((1024,768),FULLSCREEN|HWSURFACE)
                    width,height = 1024,768
                else:
                    screen = pygame.display.set_mode(size)

        if event.type == VIDEORESIZE:#用户调整窗口大小
            size = event.size
            width,height =size
            print(size)
            screen = pygame.display.set_mode(size,RESIZABLE)#重画屏幕
            
            

    position = position.move(speed)#移动图像

    if position.left < 0 or position.right > width:#检测边界，左上角(0,0)
        thing = pygame.transform.flip(thing,True,False)#翻转图像
        speed[0] = -speed[0]#反向运动

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]#反向运动

    screen.fill(bg)#填充背景
    screen.blit(thing,position)#更新图像,将thing画到screen上
    pygame.display.flip()#更新界面
    #pygame.time.delay(10)#延时
    clock.tick(100)
